/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_ssap_server_hdi.h"
#include "sle_ssap_config.h"
#include "sle_ssap_server_config.h"
#include "sle_hal_errcode.h"
#include "sle_unit_test_log.h"

static SleSsapsHdiCallbacks *g_ssapsHdiTestCbs;
static uint8_t g_serverId = 0;
static SleUuid g_serverUuid = {
    .len = 16,
};
static uint16_t g_connectId = 0;

SleHdiErrorCode SleHdiSsapsRegisterServerTest(const SleUuid *appUuid, uint8_t *serverId)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(appUuid != NULL, SLE_HAL_ERR_INVALID_PARAM, "SleHdiSsapsRegisterServerTest");
    SLE_UT_CHECK_AND_RETURN_RET_LOG(serverId != NULL, SLE_HAL_ERR_INVALID_PARAM, "SleHdiSsapsRegisterServerTest");
    *serverId = 1;
    g_serverId = *serverId;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapsUnregisterServerTest(uint8_t serverId)
{
    g_serverId = 0;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapsAddServiceTest(uint8_t serverId, const SleUuid *ServiceUuid, bool IsPrimary, uint16_t *handle)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(ServiceUuid != NULL, SLE_HAL_ERR_INVALID_PARAM, "SleHdiSsapsAddServiceTest");
    SLE_UT_CHECK_AND_RETURN_RET_LOG(handle != NULL, SLE_HAL_ERR_INVALID_PARAM, "SleHdiSsapsAddServiceTest");
    uint16_t g_serviceHandle = 1;
    *handle = g_serviceHandle;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapsAddPropertyTest(uint8_t serverId, uint16_t serviceHandle, const SsapsPropertyParam *property,
    uint16_t *handle)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(property != NULL, SLE_HAL_ERR_INVALID_PARAM, "SleHdiSsapsAddPropertyTest");
    SLE_UT_CHECK_AND_RETURN_RET_LOG(handle != NULL, SLE_HAL_ERR_INVALID_PARAM, "SleHdiSsapsAddPropertyTest");
    uint16_t g_propertyHandle = 2;
    *handle = g_propertyHandle;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapsAddDescriptorTest(uint8_t serverId, uint16_t serviceHandle, uint16_t propertyHandle,
    const SsapsDescriptorParam *descriptor, uint16_t *handle)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(descriptor != NULL, SLE_HAL_ERR_INVALID_PARAM, "SleHdiSsapsAddDescriptorTest");
    SLE_UT_CHECK_AND_RETURN_RET_LOG(handle != NULL, SLE_HAL_ERR_INVALID_PARAM, "SleHdiSsapsAddDescriptorTest");
    uint16_t g_descriptorHandle = 3;
    *handle = g_descriptorHandle;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapsStartServiceTest(uint8_t serverId, uint16_t serviceHandle)
{
    g_ssapsHdiTestCbs->OnServiceStartedCb(serverId, serviceHandle, SLE_HDI_ERRCODE_SUCC);
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapsDeleteAllServicesTest(uint8_t serverId)
{
    g_ssapsHdiTestCbs->OnAllServiceRemovedCb(serverId, SLE_HDI_ERRCODE_SUCC);
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapsSendResponseTest(uint8_t serverId, uint16_t connectId, const SsapsSendRspParam *param)
{
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapsNotifyIndicateTest(uint8_t serverId, uint16_t connectId, const SsapsNotifyParam *param)
{
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapsNotifyIndicateByUuidTest(uint8_t serverId, uint16_t connectId, const SsapsNotyfyByUuidParam *param)
{
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapsSetInfoTest(uint8_t serverId, const SsapMtuInfo *info)
{
    g_ssapsHdiTestCbs->OnMtuChangedCb(serverId, g_connectId, info, SLE_HDI_ERRCODE_SUCC);
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHdiSsapsRegisterCallbacksTest(SleSsapsHdiCallbacks *callbacks)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(callbacks != NULL, SLE_HAL_ERR_INVALID_PARAM, "SleHdiSsapsRegisterCallbacksTest");
    g_ssapsHdiTestCbs = callbacks;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode GetSleSsapsHdiOpts(SleSsapsHdiOpts *ssapsOpts)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(ssapsOpts != NULL, SLE_HAL_ERR_INVALID_PARAM, "GetSleSsapsHdiOpts");
    uint8_t uuid[UUID_LEN] = {1};
    memcpy(g_serverUuid.uuid, uuid, UUID_LEN);
    ssapsOpts->registerServer = SleHdiSsapsRegisterServerTest;
    ssapsOpts->unregisterServer = SleHdiSsapsUnregisterServerTest;
    ssapsOpts->addService = SleHdiSsapsAddServiceTest;
    ssapsOpts->addProperty = SleHdiSsapsAddPropertyTest;
    ssapsOpts->addDescriptor = SleHdiSsapsAddDescriptorTest;
    ssapsOpts->startService = SleHdiSsapsStartServiceTest;
    ssapsOpts->deleteAllServices = SleHdiSsapsDeleteAllServicesTest;
    ssapsOpts->sendResponse = SleHdiSsapsSendResponseTest;
    ssapsOpts->sendNotifyIndicate = SleHdiSsapsNotifyIndicateTest;
    ssapsOpts->sendNotifyIndicateByUuid = SleHdiSsapsNotifyIndicateByUuidTest;
    ssapsOpts->setServerMtuInfo = SleHdiSsapsSetInfoTest;
    ssapsOpts->registerServerCbks = SleHdiSsapsRegisterCallbacksTest;
    return SLE_HDI_ERRCODE_SUCC;
}